/**********
 * 功能:格式化日期
 * 参数:date:Date对象,
 * spliteBy:分割符，默认'-'
 * ***********/
function dateFormate(date, spliteBy) {
  if (!spliteBy) spliteBy = "-";
  let YY = date.getFullYear();
  let MM = date.getMonth() + 1;
  let DD = date.getDate();
  if (MM < 10) {
    MM = "0" + MM;
  }
  return YY + "-" + MM + "-" + DD;
}
/**********
 * 功能:格式化时间
 * 参数:date:Date对象,spliteBy:分割符，默认'-'
 * ***********/
function timeFormate(date, spliteBy) {
  if (!spliteBy) spliteBy = ":";
  let hh = date.getHours();
  let mm = date.getMinutes();
  let ss = date.getSeconds();
  return hh + spliteBy + mm + spliteBy + ss;
}

const date = {
  dateSplite: "-",
  timeSplite: ":",

  setDateSplite: function(splite) {
    this.dateSplite = splite;
  },
  setTimeSplite: function(splite) {
    this.timeSplite = splite;
  }
};
/**********
 * 功能:格式化时间
 * 参数:date:Date对象
 * ***********/
date.format = function(date) {
  let dateObj = date ? date : new Date();
  return (
    dateFormate(dateObj, this.dateSplite) +
    " " +
    timeFormate(dateObj, this.timeSplite)
  );
};

date.now = date.format;

date.dateFormate = dateFormate;
date.timeFormate = timeFormate;

module.exports = date;
